<?php
declare(strict_types=1);

namespace Service\SupplierService;

use App\Model\CommodityModel\SupplierCompany;
use App\Model\CommodityModel\WarehouseInfo;
use Service\BaseService;
use Service\Code\Normal;

class SupplierService extends BaseService
{
    CONST MAPPING_CLASS_PATH = "Service\\SupplierService\\SupplierMapping\\";
    /**
     * 获取消费队列需要的供应商数据
     * @param $SupplierId
     * @return array
     */
    public function getConsumerSupplierInfo($SupplierId)
    {
        $supplierInfoObj = WarehouseInfo::instance()->select([
            'w.SupplierID',
            's.SupplierCode',
            's.SupplierName',
            's.OrderDockMode',
            's.IsTryUpdateCommodityArchives',
            's.ThirdPartyPlatformCode',
            's.SyncMode',
            'w.ID AS WarehouseID',
            'w.SupplierID',
            'w.WarehouseName',
            'w.JsonBody',
            'w.CanModifyStock',
            'w.CanModifyPrice',
            's.OrderDeclare',
            's.PayDeclare',
            's.ThirdPartyPlatformCode',
            's.CommodityIsBatchDown',
            's.IsSettingFreight',
            's.IsSettingFreightTemplate',
            's.FreightTemplate',
            's.IsOneByOneMakeArchiveDoc',
            's.CustomID'
        ])
            ->from(
                WarehouseInfo::instance()->getTable() . ' as w'
            )
            ->join(SupplierCompany::instance()->getTable() . ' as s', 's.ID', '=', 'w.SupplierID')
            ->where('s.ID', $SupplierId)
            ->where('s.CompanyStatus', 1)//供应商启用状态
            ->where('w.ExamineState', 1)//仓库启用状态
            ->first();

        if (empty($supplierInfoObj)) {
            return [];
        }
        $supplierInfo = $supplierInfoObj->toArray();
        $supplierInfo['JsonBody'] = @json_decode($supplierInfo['JsonBody'], true);
        return $supplierInfo;
    }

    /**
     * 获取消费队列需要的供应商映射
     * @param $supplier array
     * @return
     */
    public function getSupplierMapping(array $supplier)
    {
        $supplierCode = $supplier['ThirdPartyPlatformCode'] ?:$supplier['SupplierCode'];
        $mappingClass = self::MAPPING_CLASS_PATH . ucfirst($supplierCode);
        if (!class_exists($mappingClass)) {
            return Normal::returnCode(Normal::FAIL, "缺少供应商商品映射");
        }
        return Normal::returnCode(Normal::SUC, "获取供应商商品映射成功",$mappingClass);
    }

}